#pragma once

#include "tm_maya_plugin.h"
#include "tm_maya_export.h"

#include "tm_tmd1_export_target.h"
#include "tm_tmd2_export_target.h"
#include "tm_stdout_maya_export_target.h"

#include <string>
#include <sstream>

using namespace std;

#define DeclareSingle( _fnptr )									\
class _DoItCmd : public MPxCommand {							\
public:															\
					_DoItCmd() {};								\
	virtual MStatus	doIt ( const MArgList& );					\
	static void*	creator();									\
};																\
void* _DoItCmd::creator()										\
{																\
	return new _DoItCmd;										\
}																\
MStatus _DoItCmd::doIt( const MArgList& args )					\
{																\
	return _fnptr(args);										\
}																\
MStatus initializePlugin( MObject _obj )						\
{																\
	MFnPlugin	_plugin( _obj );								\
	MStatus		stat;											\
	stat = _plugin.registerCommand( #_fnptr,                    \
	                                _DoItCmd::creator );	    \
	if ( !stat )												\
		stat.perror("registerCommand");							\
	return stat;												\
}																\
MStatus uninitializePlugin( MObject _obj )						\
{																\
	MFnPlugin	plugin( _obj );									\
	MStatus		stat;											\
	stat = plugin.deregisterCommand( #_fnptr );					\
	if ( !stat )												\
		stat.perror("deregisterCommand");						\
	return stat;												\
}

#define DeclareSimpleCommand( _className, _vendor, _version )	\
class _className : public MPxCommand {							\
public:															\
					_className() {};							\
	virtual MStatus	doIt ( const MArgList& );					\
	static void*	creator();									\
};																\
																\
void* _className::creator()										\
{																\
	return new _className;										\
}

DeclareSimpleCommand( tm1_export, "TimidEngineMayaPlugin", "2013");
DeclareSimpleCommand( tm2_export, "TimidEngineMayaPlugin", "2013");
DeclareSimpleCommand( timid_getverbosity, "TimidEngineMayaPlugin", "2013");
DeclareSimpleCommand( timid_setverbosity, "TimidEngineMayaPlugin", "2013");

MStatus tm1_export::doIt( const MArgList& args )
{
	cout << "Beginning FPC-ST TMD export..." << endl << endl;
	
	int nodecount = 0;
	MStatus stat;

	//count the dag nodes
	{
		MItDag dagIter( MItDag::kBreadthFirst, MFn::kInvalid, &stat );	
		for (; !dagIter.isDone(); dagIter.next())
		{
			MDagPath dagPath;
			stat = dagIter.getPath( dagPath );

			++nodecount;
		}	
		stringstream outmsg;
		outmsg << "Found DAG nodes, total count: " << nodecount;

		cout << outmsg.str().c_str() << endl << endl;
	}

	stat = ExportMayaMeshes(TTMD1ExportTarget());

	if (!stat)
	{
		cout << stat.errorString().asChar() << endl;
		setResult(stat.errorString());
	}

	return stat;
}

MStatus tm2_export::doIt( const MArgList& args )
{
	cout << "Beginning TEngine TM2 export..." << endl << endl;
	
	int nodecount = 0;
	MStatus stat;

	//count the dag nodes
	{
		MItDag dagIter( MItDag::kBreadthFirst, MFn::kInvalid, &stat );	
		for (; !dagIter.isDone(); dagIter.next())
		{
			MDagPath dagPath;
			stat = dagIter.getPath( dagPath );

			++nodecount;
		}	
		stringstream outmsg;
		outmsg << "Found DAG nodes, total count: " << nodecount;

		cout << outmsg.str().c_str() << endl << endl;
	}

	try
	{
		TTMD2ExportTarget tm2_export;
		stat = ExportMayaMeshes(tm2_export);

		if (!stat)
		{
			cout << stat.errorString().asChar() << endl;
			setResult(stat.errorString());
		}
	}
	catch (std::exception e)
	{
		setResult(e.what());
	}

	return stat;
}

MStatus timid_getverbosity::doIt( const MArgList& args )
{
	stringstream ss;
	ss << "TimidEngine exporter verbosity: " << g_TmVerbosity;
	setResult(ss.str().c_str());

	return MS::kSuccess;
}

MStatus timid_setverbosity::doIt( const MArgList& args )
{
	int newverbosity;
	MStatus stat = args.get(0, newverbosity);

	if (stat)
	{
		g_TmVerbosity = newverbosity;
		stat = MS::kSuccess;

		stringstream ss;
		ss << "TimidEngine exporter verbosity set to: " << g_TmVerbosity;
		setResult(ss.str().c_str());

		return stat;
	}
	else
		return stat;
}